package rpg.visual.mapEditor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToggleButton;

import rpg.data.XML;

public class VisualOptions extends JPanel {
	private static final long serialVersionUID = 1L;
	private VisualMapEditor visualMap;

	// Swing Variables
	private JButton swingCreateButton, swingSaveButton, swingDeleteButton;

	private ButtonGroup swingToolbarButtonGroup;
	private JToggleButton swingInsertButton, swingSelectButton, swingRemoveButton;

	// Creation
	public VisualOptions(VisualMapEditor visualMap) {
		this.setVisualMap(visualMap);
		this.setBackground(new Color(250, 250, 250));

		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

		// Toolbar
		
		// Create
		// swingCreateButton = new JButton("Create");
		// swingButtons.add(swingCreateButton);
		// swingButtons.add(Box.createHorizontalStrut(6));
		
		// Save
		swingSaveButton = new JButton(new ImageIcon("./data/icons/mini/save.png"));
		swingSaveButton.setBackground(new Color(230, 230, 230));
		swingSaveButton.setMinimumSize(new Dimension(40, 32));
		swingSaveButton.setPreferredSize(new Dimension(40, 32));
		this.add(swingSaveButton);
		this.add(Box.createHorizontalStrut(6));
		
		swingSaveButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				XML.saveMap(getVisualMap().getMap());
				JOptionPane.showMessageDialog(null, "Successfully Saved");
			}
		});
		
		// Delete
		// swingDeleteButton = new JButton("Delete");
		// swingButtons.add(swingDeleteButton);
		// swingButtons.add(Box.createHorizontalStrut(6));

		JSeparator actionSeparator = new JSeparator(JSeparator.VERTICAL);
		actionSeparator.setMaximumSize(new Dimension(4, Short.MAX_VALUE));
		this.add(actionSeparator);
		this.add(Box.createHorizontalStrut(6));

		swingToolbarButtonGroup = new ButtonGroup();
		
		// Insert
		swingInsertButton = new JToggleButton(new ImageIcon("./data/icons/mini/plus.png"));
		swingInsertButton.setBackground(new Color(230, 230, 230));
		swingInsertButton.setMinimumSize(new Dimension(40, 32));
		swingInsertButton.setPreferredSize(new Dimension(40, 32));
		swingToolbarButtonGroup.add(swingInsertButton);
		this.add(swingInsertButton);
		
		swingInsertButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				getVisualMap().setWorkAction("insert");
			}
		});

		// Insert to Select
		this.add(Box.createHorizontalStrut(6));

		// Select
		swingSelectButton = new JToggleButton(new ImageIcon("./data/icons/mini/select.png"));
		swingSelectButton.setBackground(new Color(230, 230, 230));
		swingSelectButton.setMinimumSize(new Dimension(40, 32));
		swingSelectButton.setPreferredSize(new Dimension(40, 32));
		swingSelectButton.setSelected(true);
		swingToolbarButtonGroup.add(swingSelectButton);
		this.add(swingSelectButton);

		swingSelectButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				getVisualMap().setWorkAction("select");
			}
		});
		
		// Select to Remove
		this.add(Box.createHorizontalStrut(6));
		
		// Remove
		swingRemoveButton = new JToggleButton(new ImageIcon("./data/icons/mini/remove.png"));
		swingRemoveButton.setBackground(new Color(230, 230, 230));
		swingRemoveButton.setMinimumSize(new Dimension(40, 32));
		swingRemoveButton.setPreferredSize(new Dimension(40, 32));
		swingToolbarButtonGroup.add(swingRemoveButton);
		this.add(swingRemoveButton);

		swingRemoveButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				getVisualMap().setWorkAction("remove");
			}
		});

		// Separation
		this.add(Box.createHorizontalStrut(6));
		this.add(new JSeparator(JSeparator.VERTICAL));

		this.add(Box.createHorizontalGlue());
	}

	// Get & Set
	public VisualMapEditor getVisualMap() {
		return visualMap;
	}

	public void setVisualMap(VisualMapEditor visualMap) {
		this.visualMap = visualMap;
	}

	// Get & Set Swing
	public JButton getSwingCreateButton() {
		return swingCreateButton;
	}

	public void setSwingCreateButton(JButton swingCreateButton) {
		this.swingCreateButton = swingCreateButton;
	}

	public JButton getSwingDeleteButton() {
		return swingDeleteButton;
	}

	public void setSwingDeleteButton(JButton swingDeleteButton) {
		this.swingDeleteButton = swingDeleteButton;
	}

	public JButton getSwingSaveButton() {
		return swingSaveButton;
	}

	public void setSwingSaveButton(JButton swingSaveButton) {
		this.swingSaveButton = swingSaveButton;
	}

	public JToggleButton getSwingInsertButton() {
		return swingInsertButton;
	}

	public void setSwingInsertButton(JToggleButton swingInsertButton) {
		this.swingInsertButton = swingInsertButton;
	}

	public JToggleButton getSwingSelectButton() {
		return swingSelectButton;
	}

	public void setSwingSelectButton(JToggleButton swingSelectButton) {
		this.swingSelectButton = swingSelectButton;
	}

	public ButtonGroup getSwingToolbarButtonGroup() {
		return swingToolbarButtonGroup;
	}

	public void setSwingToolbarButtonGroup(ButtonGroup swingToolbarButtonGroup) {
		this.swingToolbarButtonGroup = swingToolbarButtonGroup;
	}
}
